﻿using KamRemakeRemake.GameObjects.MapObjects;
using KamRemakeRemake.GUI;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace KamRemakeRemake.GameObjects {
    public class Camera {
        /// <summary>
        /// Global camera instance
        /// </summary>
        public static Camera Instance { get; set; }

        private Vector2 position;
        private Map map;
        private const float SPEED = 4.0f;

        private int windowWidth;
        private int windowHeight;

        public Camera(Map map, int viewPortWidth, int viewPortHeight) {
            this.map = map;
            this.windowWidth = viewPortWidth;
            this.windowHeight = viewPortHeight;
        }

        /// <summary>
        /// The current camera position
        /// </summary>
        public Vector2 Position { 
            get { return this.position; } 
            set { this.position = value; } 
        }

        /// <summary>
        /// Scrolls the camera to the given position
        /// </summary>
        public void Scroll(Direction direction) {
            switch (direction) {
                case Direction.North:
                    this.position.Y += -1 * SPEED;
                    break;
                case Direction.West:
                    this.position.X += 1 * SPEED;
                    break;
                case Direction.South:
                    this.position.Y += 1 * SPEED;
                    break;
                case Direction.East:
                    this.position.X += -1 * SPEED;
                    break;
            }

            // Make sure you can't scroll outside of the map
            position.X = MathHelper.Clamp(position.X, 0, map.WidthInPixels - windowWidth);
            position.Y = MathHelper.Clamp(position.Y, 0, map.HeightInPixels - windowHeight);
        }

        /// <summary>
        /// Updates the map
        /// </summary>
        public void Update(GameTime gameTime) {
            // Handle keyboard scrolling
            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Right)) {
                this.Scroll(Direction.West);
            }
            if (keyboardState.IsKeyDown(Keys.Left)) {
                this.Scroll(Direction.East);
            }
            if (keyboardState.IsKeyDown(Keys.Up)) {
                this.Scroll(Direction.North);
            }
            if (keyboardState.IsKeyDown(Keys.Down)) {
                this.Scroll(Direction.South);
            }

            // Handle mouse scrolling
            MouseState mouseState = Mouse.GetState();
            if (mouseState.X > windowWidth * 0.95) {
                this.Scroll(Direction.West);
                Cursor.Instance.SetCursor(Cursor.CursorType.ScrollRight);
            }
            else if (mouseState.X < windowWidth * 0.05) {
                this.Scroll(Direction.East);
                Cursor.Instance.SetCursor(Cursor.CursorType.ScrollLeft);
            }
            else if (mouseState.Y < windowHeight * 0.05) {
                this.Scroll(Direction.North);
                Cursor.Instance.SetCursor(Cursor.CursorType.ScrollUp);
            }
            else if (mouseState.Y > windowHeight * 0.95) {
                this.Scroll(Direction.South);
                Cursor.Instance.SetCursor(Cursor.CursorType.ScrollDown);
            }
            else {
                Cursor.Instance.SetCursor(Cursor.CursorType.Default);
            }
        }
    }
}
